/* qirssi
 * Copyright (c) 2009 Paulo Matias
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * Purpose: This implements the App class and instantiates an App object
 *          when the program is called.
 */

#include <QMenu>

#include "commandprocessor.h"
#include "trayhandler.h"
#include "main.h"


/* ContextMenu implementation with the only option to close the application. */
class ContextMenu : public QMenu {
public:
    ContextMenu(QApplication *app) {
        QAction *quit_action = addAction(QIcon(":/icons/quit.png"), tr("&Quit notifier"));
        quit_action->connect(quit_action, SIGNAL(triggered()), app, SLOT(quit()));
    }
};


/*
 * App class implementation.
 */

App::App(int argc, char *argv[])
    :QApplication(argc, argv)
{
    icon_on = new QIcon(":/icons/qirssi-on.png");
    icon_off = new QIcon(":/icons/qirssi-off.png");
    trayicon = new QSystemTrayIcon(*icon_off, this);
    trayhandler = new TrayHandler(this);

    if(!trayicon->isSystemTrayAvailable()) {
        fprintf(stderr, "No system tray available!\n");
        exit(1);
    }

    trayicon->setToolTip("qirssi");
    trayicon->setContextMenu(new ContextMenu(this));
    trayicon->connect(trayicon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), trayhandler, SLOT(activated(QSystemTrayIcon::ActivationReason)));
    trayicon->connect(trayicon, SIGNAL(messageClicked()), trayhandler, SLOT(reset_icon()));
    trayicon->show();
    new CommandProcessor(this);
}

App::~App() {
    delete icon_on;
    delete icon_off;
}


int main(int argc, char *argv[])
{
    App a(argc, argv);
    return a.exec();
}
